package com.exam.controller.ti;

import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.exam.common.vo.ExamTiVO;
import com.exam.common.vo.TiVO;
import com.exam.common.vo.page.PageModel;
import com.exam.entity.ExamTi;
import com.exam.service.ti.TiService;

@Controller
@RequestMapping("/ti")
public class TiController {
	
	@Autowired
	private TiService tiService;
	
	@RequestMapping(value="/tiList")
	public String tiList(Model model){
		PageModel<TiVO> page = tiService.findPage(); 
		model.addAttribute("page",page);
		return "/ti/tiList";
	}
	
	/*@RequestMapping(value="/tiDiffList")
	public String tiDiffList(){
		
	}
	
	@RequestMapping(value="/tiTypeList")
	public String tiTypeList(){
		
	}*/
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(Model model){
		Map<Integer,String> tiDiff = tiService.getTiDifficulty();
		Map<Integer,String> tiType = tiService.getTiType();
		Map<Integer,String> subject = tiService.getSubject();
		model.addAttribute("tiVO",new ExamTiVO());
		model.addAttribute("tiDiff",tiDiff);
		model.addAttribute("tiType",tiType);
		model.addAttribute("subject",subject);
		return "/ti/addTi";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(@Validated ExamTi ti,BindingResult br, Model model,RedirectAttributes redirectAttributes){
		ExamTiVO tiVO = tiService.loadByTiId(ti.getExamTiId());
		//System.out.println(tiVO);
		if(br.hasErrors()){
			Map<Integer,String> tiDiff = tiService.getTiDifficulty();
			Map<Integer,String> tiType = tiService.getTiType();
			Map<Integer,String> subject = tiService.getSubject();
			model.addAttribute("tiVO",new ExamTiVO());
			model.addAttribute("tiDiff",tiDiff);
			model.addAttribute("tiType",tiType);
			model.addAttribute("subject",subject);
			return "/ti/addTi";
		}
		if(ti != null&tiVO == null){
			tiService.save(ti);
			redirectAttributes.addFlashAttribute("message","添加成功！");
		}else{
			redirectAttributes.addFlashAttribute("message","题目编号重复！");
		}
		return "redirect:/ti/tiList";
	}
	
	@RequestMapping(value="/delete/{tiId}")
	public String delete(@PathVariable String tiId,RedirectAttributes redirectAttributes){
		ExamTi ti = new ExamTi();
		ti.setExamTiId(Integer.parseInt(tiId));
		tiService.delete(ti);
		redirectAttributes.addFlashAttribute("message","删除成功！");
		return "redirect:/tea/teacherList";
	}
	
	@RequestMapping(value="/edit/{examTiId}",method=RequestMethod.GET)
	public String edit(@PathVariable String examTiId ,Model model){
		ExamTiVO tiVO = new ExamTiVO();
		ExamTi ti = tiService.findByKey(Integer.parseInt(examTiId));
		BeanUtils.copyProperties(ti, tiVO);
		Map<Integer,String> tiDiff = tiService.getTiDifficulty();
		Map<Integer,String> tiType = tiService.getTiType();
		Map<Integer,String> subject = tiService.getSubject();
		model.addAttribute("tiDiff",tiDiff);
		model.addAttribute("tiType",tiType);
		model.addAttribute("subject",subject);
		model.addAttribute("tiVO",tiVO);
		return "/ti/editTi";
	}
	
	@RequestMapping(value="/edit/{examTiId}",method=RequestMethod.POST)
	public String edit(@PathVariable String examTiId, @Validated ExamTiVO tiVO,BindingResult br,Model model,RedirectAttributes rAttributes){
		if(br.hasErrors()){
			ExamTiVO tVO = new ExamTiVO();
			ExamTi ti = tiService.findByKey(Integer.parseInt(examTiId));
			BeanUtils.copyProperties(ti, tVO);
			model.addAttribute("tiVO",tVO);
			return "/ti/editTi";
		}
		ExamTi t = new ExamTi();
		BeanUtils.copyProperties(tiVO, t);
		//t.set
		tiService.update(t);
		rAttributes.addFlashAttribute("message","添加成功!");
		return "redirect:/ti/tiList";
	}
}
